<?php
	require("include/includes.php");
	require("include/getIP.php");
	
	// Champs obligatoires ('nom du champ' => 'nom affiché lors d'une erreur')
	$requieredFields = Array(
		'country' => 'Country', 
		'nickname' => 'Nickname', 
		'password' => 'Password', 
		'email' => 'Personnal email',
		'birthdate' => 'Date of birth');
	
	// Valeurs initiales
	$valueCountry 		= postOrCustom('country', 'France');
	$valueNickname  	= postOrCustom('nickname', '');
	$valuePassword  	= postOrCustom('password', '');
	$valueEmail  		= postOrCustom('email', '');
	$valueDisplayed_email=postOrCustom('displayed_email', '');
	$valueBirthdate 	= postOrCustom('birthdate', '');
	$valueDescription	= postOrCustom('description', '');
	$sqlBirthdate = '';
	$errors = Array();
	
	$countries = Country::getAll();
	
	/// --- VALIDATION DU FORMULAIRE ---
	if(isset($_POST['submit']))
	{ //$errors[] = 'post detected';
		if(!verifyCsrfToken())
		{
			$errors['csrf'] = 'Security token missing!';
		}
		else // CSRF ok
		{
			// vérification des champs obligatoires
			foreach($requieredFields as $reqField => $reqText)
			{
				if(!isset($_POST[$reqField]) || empty($_POST[$reqField]))
				{
					$errors[$reqField] = 'Please fill the field "'.$reqText.'"';
				}
			}
			/// vérification des champs spéciaux
			// Format de la date de naissance
			if(!empty($valueBirthdate))
			{
				$date_string = str_replace('/', '-', $valueBirthdate);
				try {
					$sqlBirthdate = new DateTime($date_string);
					$sqlBirthdate = date_format($sqlBirthdate, "Y-m-d");
				}
				catch(Exception $e) {
					$errors['birthdate'] = 'There is a mistake in your birthdate format, you should use this format: MM/DD/YYYY';
				}
			}
			// Format des emails
			if(!empty($valueEmail) && !emailIsValid($valueEmail))
			{
				$errors['email'] = 'Your private email is not a valid email adress';
			}
			if(!empty($valueDisplayed_email) && !emailIsValid($valueDisplayed_email))
			{
				$errors['displayed_email'] = 'Your public email is not a valid email adress';
			}
		}
		// Traitement des données (si aucune erreur)
		if(empty($errors))
		{
			$newPlayer = new Player(Array(
				'nickname' => $valueNickname,
				'password' => $valuePassword,
				'admin_level' => 0,
				'email' => $valueEmail,
				'displayed_email' => $valueDisplayed_email,
				'state' => 0,
				'ip' => getIP(),
				'birthdate' => $sqlBirthdate,
				'country_id' => $valueCountry,
				'description' => $valueDescription,
				'register_date' => date("Y-m-d")
			));
			
			$newPlayer->save();
			echo '--- Yeah! Ajout du joueur OKKKKKKKK! :) ---';
		}
	}
	
	$template = $twig->loadTemplate('register.html');
	$template->display(array(
		'pageTitle' 		=> 'ctr4ever - Registration',
		'contenu' 			=> 'Contenu de la page',
		'countries' 		=> $countries,
		'valueCountry'		=> $valueCountry,
		'valueNickname'		=> $valueNickname,
		'valuePassword'		=> $valuePassword,
		'valueEmail'		=> $valueEmail,
		'valueDisplayed_email'=> $valueDisplayed_email,
		'valueBirthdate'	=> $valueBirthdate,
		'valueDescription'	=> $valueDescription,
		'errors'			=> $errors,
		'crsfToken' 		=> csrfField()
	));

?>